package com.example.meetingroomnew.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.meetingroomnew.Dao.RequestDao;
import com.example.meetingroomnew.domain.Reservation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RequestServiceImpl implements RequestService{
    @Autowired
    private RequestDao requestDao;

    @Override
    public Reservation selectByid(int id) {
        Reservation reservation = requestDao.selectById(id);
        return reservation;
    }

    @Override
    public List<Reservation> selectAll() {
        List<Reservation> reservations = requestDao.selectList(null);
        return reservations;

    }

    @Override
    public List<Reservation> selectBypeople(int userId) {
        LambdaQueryWrapper<Reservation> qw=new LambdaQueryWrapper<Reservation>();
        qw.eq(Reservation::getUserId, userId);
        List<Reservation> reservations = requestDao.selectList(qw);
        return reservations;
    }

    @Override
    public int deleteByid(int id) {
        requestDao.deleteById(id);
        System.out.println("删除成功");
        return 0;

    }

    @Override
    public List<Reservation> selectByTime(Date start, Date after) {
        List<Reservation> reservations = selectAll();
        for (int i=0;i<reservations.size();i++){
            Reservation a=reservations.get(i);

//          判断申请时间是否在外面
           boolean flag=a.getEndTime().before(start)||a.getStartTime().after(after);
            if (flag){
                reservations.remove(i);
                i--;
            }

        }
        return reservations;

    }

    @Override
    public boolean Insert(Reservation reservation) {
        requestDao.insert(reservation);
        return true;
    }

    @Override
    public boolean updateRequest(Reservation reservation) {
        requestDao.updateById(reservation);
        return true;
    }
}
